<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25 0025
 * Time: 上午 10:53
 */

namespace backend\controllers;


use backend\components\BaseComponentController;
use common\models\Admin\Admin;
use yii\helpers\Url;

class LoginController extends BaseComponentController
{
    public function actionIndex()
    {
        if ( \Yii::$app->request->method == "GET" ) {
            return $this->renderPartial('index');
        }
        $admin = new Admin();
        $loginRet = $admin->login($this->post('username'), $this->post('password'), true);

        if ( $loginRet ) return $this->jsonResponse(
            self::RESPONSE_SUCCESS,
            '登陆成功', [
                self::RESPONSE_DATA_URL=>Url::to(['/site'])
            ]
        );
        return $this->jsonResponse(
            self::RESPONSE_FAILURE,
            $admin->getFirstErrorToStr()
        );
    }

    public function actionLogout()
    {
        Admin::logout();
        return $this->redirect(Url::to(["/login"]));
    }
}